﻿using UnityEngine;
using System.Collections;
using System.IO;
using System;
using System.Collections.Generic;
[System.Serializable]
public class DataMonster
{
    public int Type;
    public float Time;
    public float PosX;
}
public class GameController : MonoBehaviour
{
    public GameObject[] ArrayMonster;
    public GameObject[] ArrayBoss;

    public List<DataMonster> listData;

    public GUIText[] guiText;

    public GameObject tempObject;

    private StreamReader reader;
    private StreamWriter writer;
    private float timeCreateMonster;
    private float timeLoadData;
    private float timeGuiText;
    private static int level=1;
    private bool checkLoadData;
    private Color colorGuiText;
    private bool checkGuiText;
    private int stage;
    private bool createBoss;
    private bool bossDie;
    private bool checkDataEnemy;
    private GameObject checkBoss;
    private GameObject enemyTest;
    private Color[] arrayColor;
    private const int MaxStage = 4;


   
    void LoadData(int level)
    {
        TextAsset reader=Resources.Load< TextAsset>(   "Data/" + "Level"+level);
        string[] lines = reader.text.Split(new string[]{"\r\n"},StringSplitOptions.RemoveEmptyEntries);
        for(int i = 0; i < lines.Length; i++){
            string doc = lines[i];
            string[] Data = doc.Split('\t');

            DataMonster dataMonster = new DataMonster();
            dataMonster.Type = int.Parse( Data[0])-1;
            dataMonster.Time =float.Parse( Data[1])/1000;
            dataMonster.PosX = float.Parse(Data[2])/10;
            listData.Add(dataMonster);
        }
        


    }
    void CreateMonster(float time)
    {
        for (int i = 0; i < listData.Count; i++)
        {
            if (time >= listData[i].Time)
            {
                //Debug.Log(listData[i].Type);
              enemyTest=  Instantiate(ArrayMonster[listData[i].Type],
                    new Vector3(listData[i].PosX, 7),
                    ArrayMonster[listData[i].Type].transform.rotation) as GameObject;

                    listData.RemoveAt(i);
                    i--;
            }
            else
            {
                break;
            }
        }
    }
    void Awake()
    {
        arrayColor = new Color[2];
        for (int i = 0; i < arrayColor.Length; i++)
            arrayColor[i] = guiText[i].color;

        bossDie = true;
        createBoss = false;
        checkDataEnemy = false;
        stage = 1;
        colorGuiText = Color.white;
        checkGuiText = true;
        checkLoadData = true;
       // listData = new List<DataMonster>();


     //   LoadData(level);
        Instantiate(tempObject);

        //writer = new StreamWriter(Application.dataPath + "/Data/" + "Level3.txt", false);

        //using (writer)
        //{
        //    for (int i = 0; i < listData.Count; i++)
        //    {
        //        writer.Write(listData[i].Type + "\t" + listData[i].Time * 1000 + "\t" + listData[i].PosX * 10);
        //        writer.WriteLine();
        //    }
        //}
        //writer.Close();

    }
    void Update()
    {
        //timeCreateMonster += Time.deltaTime;

        //if (checkGuiText)
        //{
            
        //    StageText(stage.ToString(), level.ToString());
        //}




        //if (listData.Count == 0  &&!checkDataEnemy)
        //{


        //    if (level % 2 == 0 && checkBoss == null)
        //    {
        //        createBoss = true;
        //    }

        //    if (!createBoss)
        //        checkLoadData = false;
        //}
        //if (!checkLoadData)
        //{
        //    timeLoadData += Time.deltaTime;
        //    if (timeLoadData >= 5)
        //    {

        //        Debug.Log(level);
        //        if (level >= MaxStage) return;
        //        level++;
        //        LoadData(level);
        //        checkGuiText = true;
        //        timeLoadData = 0;
        //        timeCreateMonster = 0;
        //        checkLoadData = true;
        //        checkDataEnemy = false;
        //        bossDie = true;
        //        for (int i = 0; i < arrayColor.Length; i++)
        //            guiText[i].color = arrayColor[i];
        //    }
        //}
        
        //CreateMonster(timeCreateMonster);

        ////Create Boss
        //if (createBoss && checkBoss ==null)
        //{
        //    if(enemyTest==null)
        //     timeLoadData += Time.deltaTime;
        //    if (timeLoadData >= 7)
        //    {
        //        checkBoss = Instantiate(ArrayBoss[(level / 2) - 1],
        //             ArrayBoss[(level / 2) - 1].transform.position,
        //             ArrayBoss[(level / 2) - 1].transform.rotation) as GameObject;
        //        createBoss = false;
        //        bossDie = false;
        //        checkDataEnemy = true;
        //        timeLoadData = 0;
        //    }

        //} 
        ////check boss die
        //if (checkBoss == null && !bossDie  )
        //{
        //    checkLoadData = false;
        //}


        
    }

    void StageText(string stage, string level)
    {
        guiText[0].active = true;
        guiText[1].active = true;
        guiText[0].text = "Stage "+stage;
        guiText[1].text = "Level " + stage + "-" + level + "";
        guiText[0].color = Color.Lerp(guiText[0].color, colorGuiText, Time.deltaTime);
        guiText[1].color = Color.Lerp(guiText[1].color, colorGuiText, Time.deltaTime);
       
        if (guiText[0].color.r >= 0.88f)
        {
            timeGuiText += Time.deltaTime;
            if (timeGuiText >= 1)
            {
                colorGuiText = Color.clear;
            }
        }
        else if (guiText[0].color.a <= 0.2f)
        {
            checkGuiText = false;
            timeGuiText = 0;
            colorGuiText = Color.white;
            guiText[0].active = false;
            guiText[1].active = false;
        }

    }

}
